package com.bobo.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class UserInterceptor implements HandlerInterceptor{
	List<String> allowedLogin;
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		System.out.println("========================处理前=======================");
		String url = request.getRequestURI();
		Object user = request.getAttribute("user");
		if(user != null) {
			return true;
		} 
		System.out.println( "url======================" + url);
		for(String temp: allowedLogin) {
			System.out.println("interceptor1=================" + temp );
			if(url.endsWith(temp)) {
				System.out.println("interceptor2=================" + temp );
				return true;
			}
		}
		
		return false;
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView mv) throws Exception {
		System.out.println("========================处理后=======================");
	}
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
	}
	public void setAllowedLogin(List<String> allowedLogin) {
		this.allowedLogin = allowedLogin;
	}
}
